from django.db import models
from utils import BaseModel
from django.utils.safestring import mark_safe

class Country(BaseModel):
    name = models.CharField(max_length=64,verbose_name='国家名')
    img = models.ImageField(upload_to='country_img',verbose_name='国家封面')
    orders = models.IntegerField(verbose_name='排序')

    class Meta:
        db_table = "travel_country"
        verbose_name = "国家表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

    @property
    def place_list(self):
        return self.place_set.values()


    def product_list(self):
        data = self.place_list
        product_list = []
        for i in data:
            obj = Place.objects.filter(is_show=True,is_delete=False,pk=i.get('id')).first()
            lt = obj.product.values('id','title','cover','desc','price')
            for j in lt:
                product_list.append(j)
        return product_list

class Place(BaseModel):
    name = models.CharField(max_length=64,verbose_name='地区名')
    country = models.ForeignKey(to='Country', on_delete=models.DO_NOTHING, db_constraint=False,verbose_name='国家')
    orders = models.IntegerField(verbose_name='排序')
    class Meta:
        db_table = "travel_place"
        verbose_name = "地区表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    @property
    def product_list(self):
        return self.product.values('id','title','cover','desc','price')


class Product(BaseModel):
    title = models.CharField(max_length=64,verbose_name='产品标题')
    start_place = models.CharField(max_length=64,verbose_name='出发地',default='全国')
    cover = models.ImageField(upload_to='cover',verbose_name='产品封面')
    desc = models.CharField(max_length=255,verbose_name="产品简介")
    price = models.DecimalField(max_digits=8,decimal_places=2,verbose_name='价格')
    period = models.IntegerField(verbose_name="旅行周期")
    go_time = models.DateTimeField(auto_now=True,verbose_name='出发时间')
    traffic = models.BooleanField(default=True,verbose_name='是否包含交通')
    orders = models.IntegerField(verbose_name='排序')
    detail = models.TextField(verbose_name='产品详情')
    route = models.TextField(verbose_name='行程安排',null=True)
    discount = models.BooleanField(default=False,verbose_name='是否打折')
    product_category = models.ManyToManyField("Product_category",related_name='product', db_constraint=False,verbose_name="产品分类")
    place = models.ForeignKey('Place',related_name='product',on_delete=models.DO_NOTHING,db_constraint=False,null=True,verbose_name='归属地')

    class Meta:
        db_table = "travel_product"
        verbose_name = "产品表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.title

    @property
    def product_category_name(self):
        obj_lt = self.product_category.all()
        for obj in obj_lt:
            if obj.is_home == False:
                return obj.name
        return None

    @property
    def product_country(self):
        return self.place.country.name


    @property
    def place_name(self):
        return self.place.name

    # @property
    # def country_pk(self):
    #     return self.place.country.name

    @property
    def product_banner(self):
        return self.banner.values('id','img').order_by('-orders')

    @property
    def safe_detail(self):
        return mark_safe(self.detail)




class Product_banner(BaseModel):
    product = models.ForeignKey("Product",on_delete=models.DO_NOTHING, db_constraint=False, null=True,verbose_name='产品名',related_name='banner')
    name = models.CharField(max_length=255,verbose_name='图片名',null=True)
    img = models.ImageField(upload_to='product_banner')
    orders = models.IntegerField(verbose_name='排序')
    class Meta:
        db_table = "travel_product_banner"
        verbose_name = "产品轮播图表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name






class Product_category(BaseModel):
    """分类"""
    name = models.CharField(max_length=64, unique=True, verbose_name="分类名称")
    icon = models.ImageField(upload_to='category_icon',null=True)
    cover = models.ImageField(upload_to='category_img',null=True)
    is_home = models.BooleanField(default=False,verbose_name='是否是主页分类')
    orders = models.IntegerField(verbose_name='排序')
    class Meta:
        db_table = "travel_product_category"
        verbose_name = "产品分类表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    @property
    def product_list(self):
        return self.product.values('title','cover','desc','price','pk')  #子序列化